<?php

namespace app\web\controller;

use app\common\util\ErrorCode;
use app\web\service\LoginService;

class PassportController extends CommonController
{
    public function initialize()
    {
        $this->noCheckTokenAction = ["getLoginToken", "getBindMobile", "getDecryptMobile"];
        parent::initialize();
    }

    //获取请求token
    public function getLoginToken()
    {
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $service = $this->app->invokeClass(LoginService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->getLoginTokenByWx(); // 处理业务
        return $this->return_result($this->returnData);
    }

    // 解密手机号
    public function getDecryptMobile(){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $service = $this->app->invokeClass(LoginService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->decryptMobile(); // 处理业务
        return $this->return_result($this->returnData);
    }

    // 绑定手机号，创建或者更新用户信息
    public function getBindMobile(){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $service = $this->app->invokeClass(LoginService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->toBindMobile(); // 处理业务
        return $this->return_result($this->returnData);
    }

    //退出登陆
    public function logout(){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $service = $this->app->invokeClass(LoginService::class, [$this->returnData, $this->params]);
        if($service->clearToken($this->app->request->userLoginData["id"])){
            $this->returnData["msg"] = "成功退出登录";
        }else {
            $this->returnData = ErrorCode::code("fail");
            $this->returnData["msg"] = "退出登录失败";
        }
        return $this->return_result($this->returnData);
    }
}
